
#include <BViewer>
#include <BThread>
#include <BSurface>
#include <BSphere>
#include <BCamera>
#include <BViewer>
#include <BScene>
#include <BProgram>
#include <BMaterial>
#include <BHBoxLayout>
#include <BApplication>
#include <BSphereHandler>
#include <BMainWindow>
#include <BDialog>

using namespace BWE;

BGeometry* createSphere()
{
	const char* shaderVertSource =
	{
		"// microshader - colors a fragment based on its position\n"
		"varying vec4 color;\n"
		"void main(void)\n"
		"{\n"
		"    color = gl_Vertex;\n"
		"    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
		"}\n"
	};
	const char* shaderFragSource =
	{
		"varying vec4 color;\n"
		"void main(void)\n"
		"{\n"
		"    gl_FragColor = clamp( color, 0.0, 1.0 );\n"
		"}\n"
	};

	BProgram* program = new BProgram();

	BShader* shaderVert = new BShader(Shader_Vertex);
	shaderVert->setText(shaderVertSource);
	program->addShader(shaderVert);

	BShader* shaderFrag = new BShader(Shader_Fragment);
	shaderFrag->setText(shaderFragSource);
	program->addShader(shaderFrag);

	BSphere* sphere = new BSphere(5, 20, 20);
	sphere->setProgram(program);

	BMaterial* mate = new BMaterial;
	mate->setAmbient(BColor(66, 66, 66, 128));
	mate->setDiffuse(BColor(96, 136, 136, 128));
	mate->setSpecular(BColor(96, 136, 136, 128));
	mate->setShininess(0.8f);
	sphere->setMaterial(mate);

	return sphere;
}

int main(int argc, char** argv)
{
	BApplication app;

	BSceneHolder scene = new BScene();

	BNode* node = new BNode();
	BGeometry* sphere = createSphere();
	node->addShape(sphere);
	scene->inserts(node);

	BCameraHolder camera = new BCamera();
	camera->setPosition(30, 30, 30);
	camera->setCenter(0, 0, 0);

	BViewer* viewer = new BViewer();
	viewer->setScene(scene);
	viewer->setCamera(camera.get());
	viewer->setHandler(new BSphereHandler);
	viewer->setRect(50, 50, 800, 520);
	viewer->setBorder(10);
	viewer->setPerchs(5, 5, 5, 5);
	viewer->setMargins(5, 5, 5, 5);
	viewer->setStyleSheet("color:rgb(66, 66, 66)");

	BHBoxLayout* hlayout = new BHBoxLayout();
	hlayout->addWidget(viewer);
	
	BDialog dialog;
	dialog.setSize(500, 400);
	dialog.setMargins(2, 2, 2, 2);
	dialog.setLayout(hlayout);
	dialog.show();

	return app.execute();
}
